package com.app.Cutofftime;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import androidx.annotation.Nullable;
import java.util.Calendar;
import java.util.TimerTask;
import java.util.Timer;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;


public class ClockView extends View {

    private Paint paint;
    private float r=50;
    private double flow=Math.PI;
    private int indicatorSize=15;
    private String[] hours=new String[]{"12","1","2","3","4","5","6","7","8","9","10","11"};
    private Calendar mCalendar;
    private float tx;
    private float ty;
    private float sx;
    private float sy;
    private int ho;
    private int m;
    private int s;
    private double angle;

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                invalidate();
            }
        },0,1000);
    }

    @SuppressLint("ResourceAsColor")
    private void initPaint() {
        paint =new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(R.color.dark);
        mCalendar=Calendar.getInstance();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int w=getWidth();
        int h=getHeight();
        r=w/2-60;
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(w/2,h/2,r, paint);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(w/2,h/2,20, paint);
        paint.setStyle(Paint.Style.STROKE);
        for(int i=0;i<hours.length;i++){
            paint.setStrokeWidth(5);
            angle=-Math.PI*30*i/180+flow;
            tx=(w/2+(float)Math.sin(angle)*(r-indicatorSize));
            ty= (h/2+(float)Math.cos(angle)*(r-indicatorSize));
            sx=(float)(w/2+Math.ceil(Math.sin(angle)*r));
            sy=(float)(h/2+Math.ceil(Math.cos(angle)*r) );

            canvas.drawLine(tx,ty,sx,sy, paint);

            paint.setStrokeWidth(5);
            for(int j=0;j<5;j++){
                angle=-Math.PI*(30*i+j*6)/180+flow;
                tx=(w/2+(float)Math.sin(angle)*(r-indicatorSize/2));
                ty= (h/2+(float)Math.cos(angle)*(r-indicatorSize/2));
                sx=(float)(w/2+Math.ceil(Math.sin(angle)*r));
                sy=(float)(h/2+Math.ceil(Math.cos(angle)*r) );
                paint.setStrokeWidth(5);
                canvas.drawLine(tx,ty,sx,sy, paint);

            }
            paint.setStrokeWidth(1);
            angle=-Math.PI*30*i/180+flow;
            tx=(float)(w/2+Math.sin(angle)*(r-60));
            ty=(float) (h/2+Math.cos(angle)*(r-60));
            paint.setStrokeWidth(1);
            paint.setTextSize(40);
            canvas.drawText(hours[i],tx- paint.measureText(hours[i])/2,ty+ paint.getTextSize()/2, paint);
        }

        mCalendar.setTimeInMillis(System.currentTimeMillis());
        ho=mCalendar.get(Calendar.HOUR);
        m=mCalendar.get(Calendar.MINUTE);
        s=mCalendar.get(Calendar.SECOND);

        paint.setStrokeWidth(20);
        angle=-(Math.PI*30*(ho+m/60f))/180+flow;
        canvas.drawLine(w/2,h/2,(float)(w/2+Math.sin(angle)*(r*5f/12)),
                (float) (h/2+Math.cos(angle)*(r*5f/12)), paint);

        paint.setStrokeWidth(10);
        angle=-(Math.PI*6*(m+s/60f))/180+flow;
        canvas.drawLine(w/2,h/2,(float)(w/2+Math.sin(angle)*(r*7f/12)),
                (float) (h/2+Math.cos(angle)*(r*7f/12)), paint);

        paint.setStrokeWidth(5);
        angle=-(Math.PI*6*s)/180+flow;
        canvas.drawLine(w/2,h/2,(float)(w/2+Math.sin(angle)*(r*17f/24)),
                (float) (h/2+Math.cos(angle)*(r*17f/24)), paint);



    }


}

